//<SCRIPT>
function Files(){
	this.__fileID = "divFile";
	this.__pageCount = "lblPageCount";
	this.__pageID = "divPage";
	this.__pagesID = "divPages";
	this.__fileContID = "contFile";
	this.__container = "container";
	this.__selectBGColor = "navy";
	this.__selectColor = "white";
	this.__typeNA = 0;
	this.__typePage = 1;
	this.__typeFile = 2;

	this.overFile = overFile;
	this.overPage = overPage;
	this.preSelect = preSelect;
	this.mouseDown = mouseDownFile;
//	this._selectFile = _selectFile;
	this._isFileCont = _isFileCont;
	this._prevFileCont = _prevFileCont;
	this._nextFileCont = _nextFileCont;
	this.nextPage = _nextPage;
	this._isBrother = _isBrother;
	this._isCousin = _isCousin;
	this._isFile = _isFile;
	this._isPage = _isPage;
	this._isPoF = _isPoF;
	this._isFax = _isFax;
	this._type = _type;
	this._isSelected = _isSelected;
	this._fileCont = _fileCont;
	this._fileConts = _fileConts;
	this.select = select;
	this.unSelect = unSelect;
	this.selectIt = selectIt;
	this.selectRange = selectRange
	this._fileName = _fileName;
	this._pagesCont = _pagesCont;
	this._pageCount = _pageCount;
	this._pageNum = _pageNum;
	this._container = _container;
	//this._contID = _contID;
	this.getNode = getNode;
	this.getFileNode = getFileNode;
	this.getPageNode = getPageNode;
	this.showPages = showPages;
	this.showPagesTable = showPagesTable;
	this.colorImageFolders = colorImageFolders;
	this.colorImageFolder = colorImageFolder;
	this._xmlPagesDSO = _xmlPagesDSO;
	this._rsPages = _rsPages;
	this.showFiles = showFiles;
	this.pageNumbers = pageNumbers;

	var p;
	for( var a=0; a < arguments.length; a++ ){
		var params = arguments[a];
		for( p in params )
			this[p] = params[p];
	}

	this.hist = this.__histObj;
	///
	//******* Event handlers *******
	function mouseDownFile(){
		this.select();
	}
	function overFile(out){
		var e = this._isPage(e) || this._isFile();
		if( e )e.style.textDecorationUnderline = !out;
		//this.overPage(out);
	}
	function overPage(out){
		var e = this.getPageNode();	if( !e )return;	e.style.textDecorationUnderline = !out;
	}
	function preSelect(e){
		if( !defined(e) )e = event.srcElement;
		if( event.button == 2 )return;
		if( !this._isPage(e) && !this._isFile(e) )return;
//		var r = document.body.createTextRange(); r.moveToElementText(e); r.select();
		try{
			if( false && e )setTimeout("document.selection.empty();",1000);
			else{
				var r = document.body.createTextRange();
				r.moveToElementText(e); r.findText('~~'); r.select();
			}
//			setTimeout("status='TextRange.length:"+document.selection.TextRange.length+"'",1000)
		}catch(exp){/*setTimeout("status='"+exp.description.replace(/'/g,'"')+"'",1000)*/}
	}
	//******* Methods *******
	function showFiles(el,show){
		var contFiles = el.all[this.__fileContID];
		for( var i=0; contFiles && i < contFiles.length; i++ )
			this.showPages(contFiles[i],show);
	}
	function getFileNode(){return this.getNode(this.__fileID);}
	function getPageNode(){return this.getNode(this.__pageID);}
	function getNode(id){
		var e = window.event.srcElement.id == id ? window.event.srcElement : null;
//		if( e ) window.event.cancelBubble = true;
		return e;
	}
	/****************************** Select *******************************/
	function select(e,not,ctrlKey,shiftKey){
		if( typeof(e) == "number" ){
			e = this.hist.Get(e);
			if( !e )return;
		}
		e = this._isPoF(e || event.srcElement);
		if( shiftKey )return this.selectRange(this.hist.Get(),e);
		return this.selectIt(e,not || (ctrlKey && this.hist.Get(e)),ctrlKey);
	}
	function unSelect(){
		while( this.hist.Length() )
			this.select(this.hist.Get(0,true),true);
	}
	function selectRange(elStart,elEnd){
		var pageStart = this._pageNum(elStart);
		var pageEnd = this._pageNum(elEnd);
		var el = elStart;
		if( pageStart > pageEnd ){
			el = elEnd;
			elEnd = elStart;
			elStart = el;
		}
		for( ; el != this.nextPage(elEnd) ; el = this.nextPage(el) )
			while( !this.select(el,undefined,true) ){}
		for( ;el && this.hist.Get(el); el = this.nextPage(el) )
			this.hist.Put(el);
		return elStart;
	}
	function selectIt(e,not,ctrlKey){//debugger;
		e = this._isPoF(e || event.srcElement);
		var anyKey = ctrlKey;
		if( !e )return null;
		if( this._isFile(e) )anyKey = false;
		if( not ){
			if( defined(e.b) ){
				e.style.backgroundColor = e.b;
				e.style.color = e.c;
			}
			this.hist.Pop(e);
			return null;
		}else	if( !this.hist.Get(e) ){
			if( !defined(e.b) ){
				e.b = e.style.backgroundColor;
				e.c = e.style.color;
			}
			e.style.backgroundColor = this.__selectBGColor;
			e.style.color = this.__selectColor;
			for( var he=this.hist.First(); he && arguments.callee != selectIt.caller; ){
				if( !this._isPage(he) || !anyKey || !this._isBrother(e,he) ){
					this.selectIt(he,true);
					he = this.hist.This();
				}else he = this.hist.Next()
			}
			this.hist.Put(e);
		}
		return e;
	}
	function _isSelected(e){
		e = this._isPoF(e || event.srcElement);
		return e && e.style.backgroundColor?true:false;
	}
	/************************* Pages ****************************/
	function pageNumbers(el){
		return this.hist.FindPos(el) >= 0 ? this.hist.Pages() : [this._pageNum(el)];
	}
	
	function showPages(e,show){
		e = this._pagesCont(e);
		if( !defined(show) )return e.style.display==""?true:false;
		if( show.xml )show = show.xml;
		if( typeof(show) != "string" || updateXmlDSO(this._xmlPagesDSO(e),show) == 1 )
			e.style.display = !show?"none":"";
		this.colorImageFolder(e);
	}
	function colorImageFolders(e){
		var els = this._fileConts(e);
		for( var i=0; i < els.length; i++ )
			this.colorImageFolder(els[i]);
	}
	function colorImageFolder(e){
		var lbl = this._fileCont(e);//,"dataFld","FileName");
		if( !lbl.countClass )return;
		var countClass = " "+lbl.countClass;
		lbl.className = lbl.className.replace(countClass,"");
		if( this._pageCount(e) > 0 )lbl.className += countClass;
	}
	function showPagesTable(tbl){
		if( tbl.readyState != "complete" )return;
		tbl.style.display = isXmlRSEmpty(eval(tbl.dataSrc.substr(1)).recordset) ? "none" : "";
	}
	function _pageCount(e,count){
		if( arguments.length > 1 )alert(getFuncName()+":count argument used!!!");
		var rs = this._rsPages(e);
		return isXmlRSEmpty(rs) ? (parseInt(rsField(this._fileCont(e),"Count"))||0) : rs.recordCount;
	}
	function _rsPages(e){return this._xmlPagesDSO(e).recordset}
	function _xmlPagesDSO(e){return eval(this._pagesCont(e).dataSrc.substr(1))}

	//************************************ Properties ****************************************
	function _isFax(el){
		var fileName = this._fileName(el);
		return fileName && fileName.match(/^\d\d_\d{6}/);
	}
	function _prevFileCont(e){return this._fileCont(e).previousSibling}
	function _nextFileCont(e){return this._fileCont(e).nextSibling}
	function _isFileCont(e){debugger;
		return (e||event.srcElement) && (e||event.srcElement).id == this.__fileContID 
	}
	function _isPoF(e){return this._isPage(e) || this._isFile(e)}
	function _isFile(e){return !this._isPage(e) && getParentByAttr(e||event.srcElement,"id",this.__fileID)}
	function _isPage(e){return getParentByAttr(e||event.srcElement,"id",this.__pageID)}
	function _type(e){return this._isPage(e) ? 1 : this._isFile(e) ? 2 : this._typeNA}
	function _isCousin(e1,e2){ return this._container(e1) == this._container(e2) }
	function _isBrother(e1,e2){ return this._fileName(e1) == this._fileName(e2) }
	function _container(e){return getParentByAttr(e||event.srcElement,this.__container)}
	//function _contID(e){return this._container(e).id}
	function _fileCont(e){return getParentByAttr(e,"id",this.__fileID)}
	function _fileConts(e){return this._container(e).all[this.__fileID]}
	function _pagesCont(e){return this._fileCont(e).all[this.__pagesID]}
	function _fileName(e){return rsField(e,"FileName").value}
	function _nextPage(e){
		if( !this._isPage(e) )return alert("["+(e||{}).outerHTML+"] is not a page.");
		return ((getParentTBODY(e).nextSibling||{}).all||{})[this.__pageID];
	}
	function _pageNum(e,num){
		e = e || event.srcElement;
		if( defined(num) ){
			if( num == PAGES_ALL )return this._fileCont(e);
			var pg,pgs = this._pagesCont(e).all[this.__pageID];
			
			pg = defined(pgs && pgs.length)?pgs[Math.min(pgs.length,num)-1]:(pgs||null);
			return pg;
		}
		return rsField(e,"PageNumber").value;//?this._isFile(e)?PAGE_FOLDER_NUM_NA:alert(e.innerText+" is not a number."):parseInt(e.innerText);
	}
}
/////////////////////////////////////////////////////
function SelectHistory(){
	this.__OBJ_NAME = "SelectHistory";
	this.__NO_POS = Number.NEGATIVE_INFINITY;
	this.__INV_PARAM = ": Invalid parameter.";
	this.__hist = new Array();
	this.__iterator = 0;
	this.Length = Length;// function(){return this.__hist.length};
	this.Put = Put;
	this.Get = Get;
	this.Pop = Pop;
	this.Pages = Pages;
	this.Join = Join;
	this.First = First;
	this.Next = Next;
	this.This = This;
	this.FindPos = FindPos;
	//Definitions
	function First(){return this.Get(this.__iterator=0);}
	function Next(){return this.Get(++this.__iterator);}
	function This(){return this.Get(this.__iterator);}
	function Length(){
		for( var i=0; i < this.__hist.length; i++ )
			this.Get(i);
		return this.__hist.length;
	}
	function Pages(el){
		var a = new Array();
		for( var i=0,l=this.Length(); i < l; i++ )
			a.unshift(parseInt(rsField(this.Get(i),"PageNumber"))||PAGES_ALL);
		return a.length ? a : el ? [el] : a;
	}
	function Join(){
		return this.Pages().join(",");
	}
	function Put(e){
		if( this.Get(e) )this.Pop(e);
		this.__hist.unshift(e); 
	}
	function Get(e,rem){
		var ret;
		if( arguments.length == 0 )e = 0;
		if( e==0 || e ){
			if( !isNaN(e) )ret = e == this.__NO_POS ? null : this.__hist[e<0?this.__hist.length+e:e]||null;
			else if( typeof(e) == "object" )ret = this.Get(this.FindPos(e));
			else return alert(getFuncName()+":Unknown argument type.\nArgument - (e)")||null;
		}
		if( !defined(ret) ){debugger;
			return alert( this.__OBJ_NAME+"->"+"Get("+((e||{}).innerHTML||e)+")"+this.__INV_PARAM);
		}
		if( rem && ret != null || ret && !ret.parentElement )this.Pop(ret);
		return ret == 0 || ret && typeof(ret) == 'object' && ret.parentElement ? ret : null;
	}
	function Pop(e){
		if( arguments.length == 0 )e = 0;
		if( e==0 || e ){
			if( !isNaN(e) )return e == this.__NO_POS ? null : this.__hist.splice(e<0?this.__hist.length+e:e,1)[0];
			if( typeof(e) == "object" )return this.Pop(this.FindPos(e));
		}
		debugger;
		alert( this.__OBJ_NAME+"->"+"Pop("+((e||{}).innerHTML||e)+")"+this.__INV_PARAM);
	}
	function FindPos(e){
		for( var i=0,a=this.__hist; i < a.length; i++ )
			if( a[i] == e )return i;
		return this.__NO_POS;
	}
}
